package com.kyzh.core.pager.weal.quanzi

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.DynamicBean
import com.gushenge.core.requests.QuanZiRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemDynamicDiscuss3Binding
import com.kyzh.core.dialog.showSendMessageDialog
import com.kyzh.core.utils.startLogin
import com.shuyu.gsyvideoplayer.GSYVideoManager
import kotlinx.android.synthetic.main.activity_comment_detail.*
import kotlinx.android.synthetic.main.activity_comment_detail.close
import kotlinx.android.synthetic.main.activity_comment_detail.rvList1
import kotlinx.android.synthetic.main.activity_comment_detail.tvDiscuss
import kotlinx.android.synthetic.main.activity_comment_detail.tvSubmit
import kotlinx.android.synthetic.main.activity_comment_detail.tvTitle
import kotlinx.android.synthetic.main.activity_pinglun_detail.*

class PingLunErJiDetailActivity : BaseActivity()  {
    companion object{
        @JvmStatic
        fun start(context: Activity,dynamic: DynamicBean,dynamicId:String) {
            val starter = Intent(context, PingLunErJiDetailActivity::class.java).putExtra("data",dynamic).putExtra("dynamicId",dynamicId)
            context.startActivityForResult(starter,10000)
        }
    }
    private var id:String = ""
    private var dynamic: DynamicBean? =null
    private var list : ArrayList<DynamicBean> = arrayListOf()
    private val adapter: PingLunAdapter = PingLunAdapter(list)
    private var dataBindTop:ItemDynamicDiscuss3Binding ? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pinglun_detail)

        initView()
        initData()
    }

    private lateinit var  dynamicId:String
    fun initView(){
        dynamicId = intent.getStringExtra("dynamicId")
        dynamic= intent.getSerializableExtra("data") as DynamicBean
        close.setOnClickListener {
            if(click){
                setResult(RESULT_OK)
            }
            finish()
        }
        tvDiscuss.setOnClickListener {
            if(startLogin()){
                showSendMessageDialog {
                    QuanZiRequest.sendDynamicPingLun(dongtai_id =dynamicId,content =this,pid = dynamic!!.id){
                        if(this){
                            initData()
                        }
                    }
                }
            }

        }
        initTop()
        rvList1.layoutManager = object : LinearLayoutManager(this, RecyclerView.VERTICAL,false){        }
        rvList1.adapter = adapter
        adapter.removeAllHeaderView()
        adapter.addHeaderView(dataBindTop!!.root)
    }

    //  加载顶部 view
    fun initTop(){
        dataBindTop = DataBindingUtil.inflate<ItemDynamicDiscuss3Binding>(
            layoutInflater,
            R.layout.item_dynamic_discuss3,
            null,
            true
        )
        dataBindTop?.apply {
            swipe.setOnRefreshListener {
                initView()
                initData()
                swipe.isRefreshing = false
            }
            dynamic?.let {
                data = it
                tvContent.text = it.content
                tvSubmit.hint = "回复：${it.pet_name}"
                Glide.with(this@PingLunErJiDetailActivity).load(it.face).into(ivHead)
                if(it.color.isNullOrEmpty()){
                    tvName .setHasColors(false,null)
                    tvName.setText(it.pet_name)
                }else{
                    if(it.color.size == 1){
                        tvName.setText(it.pet_name)
                        tvName .setHasColors(false,null)
                        tvName.setTextColor(Color.parseColor(it.color[0]))
                    }else{
                        var intArray = IntArray(it.color!!.size)
                        it.color?.forEachIndexed { index, s ->
                            intArray[index] = Color.parseColor(s)
                        }
                        tvName.setTextColor(Color.parseColor(it.color[0]))
                        tvName.setText(it.pet_name)
                        tvName .setHasColors(true,intArray)

                    }
                }
                if(it.is_zan==1){
                    //点赞了
                    val drawable = resources.getDrawable(R.drawable.ic_zan1)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                }else{
                    //为点赞
                    val drawable = resources.getDrawable(R.drawable.ic_nozan)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                }
                tvZan.setOnClickListener { view->
                    click = true
                    QuanZiRequest.DynamicZan(dynamic!!.id,"1"){
                        if(it.is_zan==1){
                            val i = it.zan.toInt() - 1
                            it.zan = i
                            it.is_zan =0
                        }else{
                            val i =it.zan.toInt() + 1
                            it.zan = i
                            it.is_zan =1
                        }
                        tvZan.text = it.zan.toString()
                        if(it.is_zan==1){
                            //点赞了
                            val drawable = resources.getDrawable(R.drawable.ic_zan1)
                            tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                        }else{
                            //为点赞
                            val drawable = resources.getDrawable(R.drawable.ic_zan1)
                            tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                        }
                    }

                }
            }
        }
    }
    fun initData(){
        if (dynamic ==null){
            return
        }else{
            val inflate = LayoutInflater.from(this@PingLunErJiDetailActivity).inflate(R.layout.empty, null)
            val tv1 = inflate.findViewById<TextView>(R.id.tv1)
            tv1.text = "暂无评论"
            id =dynamic!!.id
            QuanZiRequest. getDynamicZiPingLun(id){
                //
                this?.apply {
                    tvTitle.text = "${data.size}条评论"
                        adapter.setNewInstance(data)
                }
            }
        }
    }

    inner class  PingLunAdapter(data : ArrayList<DynamicBean>) : BaseQuickAdapter<DynamicBean,BaseDataBindingHolder<ItemDynamicDiscuss3Binding>>(R.layout.item_dynamic_discuss3,data){
        override fun convert(
            holder: BaseDataBindingHolder<ItemDynamicDiscuss3Binding>,
            item: DynamicBean
        ) {
            holder.dataBinding?.apply {
                data = item
                var pinglun :String =if(TextUtils.equals(item.pet_name,item.huifu)){
                    item.content
                }else if (TextUtils.isEmpty(item.huifu)|| TextUtils.equals("null",item.huifu)){
                    item.content
                }else{
                    "${item.huifu}${item.content}"
                }
                val spannableString1 = SpannableStringBuilder(pinglun)
                val span1: ClickableSpan = object : ClickableSpan() {
                    override fun onClick(widget: View) {
//                      GameCommentActivity.start(context as Activity, SpConsts.gid,item.id ,ppid,1,pinglun1)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.color = context.resources.getColor(R.color.font_33)
                        ds.isFakeBoldText = true
                        ds.isUnderlineText = false
                    }
                }
                if(item.color.isNullOrEmpty()){

                    tvName .setHasColors(false,null)
                    tvName.setText(item.pet_name)
                }else{
                    if(item.color.size == 1){
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(false,null)
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                    }else{
                        var intArray = IntArray(item.color!!.size)
                        item.color?.forEachIndexed { index, s ->
                            intArray[index] = Color.parseColor(s)
                        }
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(true,intArray)

                    }
                }
                spannableString1.setSpan(span1, 0, item.huifu.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
                tvContent.text = spannableString1
                tvContent.movementMethod = LinkMovementMethod.getInstance()
                //GameDetailPinglunAdapter
                root.setOnClickListener {
                    showSendMessageDialog("回复@${item.pet_name}") {
                        QuanZiRequest.sendDynamicPingLun(dongtai_id =dynamicId,content =this,pid = dynamic!!.id,ppid = item.id){
                            if(this){
                                initData()
                            }
                        }
                    }
                }
                if(item.is_zan==1){
                    //点赞了
                    val drawable = resources.getDrawable(R.drawable.ic_zan1)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                }else{
                    //为点赞
                    val drawable = resources.getDrawable(R.drawable.ic_nozan)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                }
                tvZan.setOnClickListener {
                    click = true
                        QuanZiRequest.DynamicZan(item.id,"1"){
                            if(item.is_zan==1){
                                val i = item.zan.toInt() - 1
                                item.zan = i
                                item.is_zan =0
                            }else{
                                val i =item.zan.toInt() + 1
                                item.zan = i
                                item.is_zan =1

                            }
                            tvZan.text = item.zan.toString()
                            if(item.is_zan==1){
                                //点赞了
                                val drawable = resources.getDrawable(R.drawable.ic_zan1)
                                tvZan.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)

                            }else{
                                //为点赞
                                val drawable = resources.getDrawable(R.drawable.ic_nozan)
                                tvZan.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
                            }
                            notifyItemChanged(holder.position)
                        }

                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }
    private var click:Boolean = false
    override fun onBackPressed() {
        if(GSYVideoManager.isFullState(this)){
            GSYVideoManager.backFromWindowFull(this)
        }else{
            if(click){
                setResult(RESULT_OK)
            }
            finish()
        }

    }
    override fun onDestroy() {
        super.onDestroy()
    }


}